package com.aij.starter.api;

import org.springframework.util.StringUtils;

import com.aij.starter.event.ChatApiEventHandler;
import com.alibaba.fastjson.JSON;

/**
 * 对接ChatGLM Api接口
 * @author aij
 */
public class ChatGlmApi {
    
    public static class Request {
    	public String prompt;
    }
    
    public static class Response {
    	public String response;
    	public String status;
    }
	
    public interface OnNewWord {
        void callback(String text);
    }

    static String url = "";
    static HttpApiClient client = null;
    public static void Initialize(String model)
    {
    	url = model;
    	client = new HttpApiClient();
    }
    public static void Initialize(String model, String prompt)
    {
    	url = model;
    }

    public static void Run(String word)
    {
    	Request req = new Request();
    	req.prompt = word;
		String result = client.doPost(url, req);
    	if (!StringUtils.isEmpty(result)) {
    		Response res = JSON.parseObject(result, Response.class);
    		String response = res.response;
    		if (response != null && !response.isEmpty()) {
	        	ChatApiEventHandler handler = new ChatApiEventHandler();
	        	handler.callback(response);
    		}
    	}
    }

    public static void Destroy()
    {
    	client = null;
    }
 
	public static void main(String[] args) {
		String url = "http://192.168.0.101:8000/";
		ChatGlmApi.Initialize(url);
		ChatGlmApi.Run("你好");
		ChatGlmApi.Destroy();
	}
}
